Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Sep 28, 2025

Link issues

fixes #6798

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Refactor CssBuilder to improve performance by replacing the internal string list with a StringBuilder and unifying style handling, and update all component usage accordingly

Enhancements:

  • Replace CssBuilder’s List-based buffer with a StringBuilder and a hasContent flag to optimize string concatenation
  • Remove AddStyle overloads and unify conditional style injection via AddClass, updating all components to use AddClass for CSS variable and inline style building
  • Use Convert.ToString and InvariantCulture when appending attribute values and numeric styles to ensure consistent formatting
  • Replace LINQ .Any() with direct Count check in Captcha to reduce allocation overhead and introduce IsVertical helper in Drawer for clearer orientation logic

Tests:

  • Remove obsolete AddStyle unit tests from CssBuilderTest to align with the new API

Copilot AI review requested due to automatic review settings September 28, 2025 03:06
@bb-auto bb-auto bot added the enhancement New feature or request label Sep 28, 2025
@bb-auto bb-auto bot added this to the 9.10.0 milestone Sep 28, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Sep 28, 2025

Reviewer's Guide

This PR refactors the CssBuilder implementation for better performance by switching to StringBuilder, removes legacy AddStyle methods, standardizes attribute value conversion, updates component code to use the new AddClass API for styling, and aligns unit tests with these changes.

Class diagram for refactored CssBuilder

classDiagram
class CssBuilder {
  - StringBuilder _builder
  - bool _hasConent
  + CssBuilder(string? value)
  + CssBuilder AddClass(string? value)
  + CssBuilder AddClass(string value, bool when)
  + CssBuilder AddClass(string value, Func<bool> when)
  + CssBuilder AddClassFromAttributes(IDictionary<string, object>? additionalAttributes)
  + CssBuilder AddStyleFromAttributes(IDictionary<string, object>? additionalAttributes)
  + string? Build()
}

class StringBuilder
CssBuilder --> StringBuilder
Loading

Class diagram for affected component style properties

classDiagram
class ShieldBadge {
  - string? StyleString
}
class Captcha {
  - string? StyleString
  - string? FailedStyle
}
class Drawer {
  - string? StyleString
  - string? DrawerStyleString
  - bool IsVertical
}
class AutoFill {
  - string? PlaceHolderStyleString
}
class Card {
  - string? HeaderStyleString
}
class EditorForm {
  - string? FormStyleString
}
class BootstrapLabel {
  - string? StyleString
}
class Logout {
  - string? AvatarStyleString
}
class NavbarGroup {
  - string? StyleString
}
class Toolbar {
  - string? StyleString
}
class ValidateForm {
  - string? StyleString
}

ShieldBadge --> CssBuilder
Captcha --> CssBuilder
Drawer --> CssBuilder
AutoFill --> CssBuilder
Card --> CssBuilder
EditorForm --> CssBuilder
BootstrapLabel --> CssBuilder
Logout --> CssBuilder
NavbarGroup --> CssBuilder
Toolbar --> CssBuilder
ValidateForm --> CssBuilder
Loading

File-Level Changes

Change Details Files
Refactor CssBuilder internal implementation
  • Replaced List buffer with a StringBuilder and a boolean _hasContent flag
  • Updated AddClass to append values with proper spacing using StringBuilder
  • Changed Build() to return builder.ToString() when content exists
  • Removed all AddStyle overloads and related methods
src/BootstrapBlazor/Utils/CssBuilder.cs
Standardize attribute value conversion
  • Imported System.Globalization
  • Replaced c?.ToString() calls with Convert.ToString(c, CultureInfo.InvariantCulture) in class/style attribute methods
src/BootstrapBlazor/Utils/CssBuilder.cs
Replace AddStyle usages with AddClass in components
  • Swapped AddStyle calls for AddClass with formatted CSS strings and semicolons
  • Introduced IsVertical helper property in Drawer for height/width logic
  • Updated style-building logic across multiple Razor components to use new API
src/BootstrapBlazor/Components/Badge/ShieldBadge.razor.cs
src/BootstrapBlazor/Components/Captcha/Captcha.razor.cs
src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs
src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs
src/BootstrapBlazor/Components/Card/Card.razor.cs
src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs
src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs
src/BootstrapBlazor/Components/Logout/Logout.razor.cs
src/BootstrapBlazor/Components/Navbar/NavbarGroup.razor.cs
src/BootstrapBlazor/Components/Toolbar/Toolbar.razor.cs
src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs
Align unit tests with API changes
  • Removed obsolete AddStyle tests in CssBuilderTest
  • Updated DrawerTest to assert new CSS variable syntax and absence of old style markers
test/UnitTest/Utils/CssBuilderTest.cs
test/UnitTest/Components/DrawerTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#6798 Improve the performance of CssBuilder by using StringBuilder instead of List for string concatenation.
#6798 Refactor usages of CssBuilder in the codebase to be compatible with the new StringBuilder-based implementation.
#6798 Update or remove related tests to reflect the new implementation and ensure correctness.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang merged commit 8422a3f into main Sep 28, 2025
4 of 5 checks passed
@ArgoZhang ArgoZhang deleted the feat-style branch September 28, 2025 03:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves performance of the CssBuilder class by replacing the internal List with StringBuilder for CSS generation. The change eliminates multiple string concatenations in favor of a more efficient single-buffer approach.

  • Replace List with StringBuilder in CssBuilder for better performance
  • Remove unused AddStyle methods to simplify the API surface
  • Update component code to use AddClass instead of removed AddStyle methods

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/BootstrapBlazor/Utils/CssBuilder.cs Core performance improvement - replaced List with StringBuilder and removed AddStyle methods
test/UnitTest/Utils/CssBuilderTest.cs Removed tests for deleted AddStyle methods
test/UnitTest/Components/DrawerTest.cs Updated test assertions to match new CSS variable output format
src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs Changed AddStyle to AddClass call
src/BootstrapBlazor/Components/Toolbar/Toolbar.razor.cs Changed AddStyle to AddClass call
src/BootstrapBlazor/Components/Navbar/NavbarGroup.razor.cs Changed AddStyle to AddClass call
src/BootstrapBlazor/Components/Logout/Logout.razor.cs Changed AddStyle to AddClass call
src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs Changed AddStyle to AddClass call
src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs Changed AddStyle to AddClass call
src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs Changed AddStyle to AddClass calls and improved logic with IsVertical property
src/BootstrapBlazor/Components/Card/Card.razor.cs Changed AddStyle to AddClass call with null check
src/BootstrapBlazor/Components/Captcha/Captcha.razor.cs Changed AddStyle to AddClass calls and improved LINQ usage
src/BootstrapBlazor/Components/Badge/ShieldBadge.razor.cs Changed AddStyle to AddClass calls
src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs Changed AddStyle to AddClass call with culture-invariant formatting

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +16 to +17
private readonly StringBuilder _builder = new();
private bool _hasConent;
Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field name _hasConent has a spelling error. It should be _hasContent.

Copilot uses AI. Check for mistakes.
if (!string.IsNullOrEmpty(value))
{
_builder.Append(value);
_hasConent = true;
Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field name _hasConent has a spelling error. It should be _hasContent.

Copilot uses AI. Check for mistakes.
if (!string.IsNullOrEmpty(value)) stringBuffer.Add(value);
if (!string.IsNullOrEmpty(value))
{
if (_hasConent)
Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field name _hasConent has a spelling error. It should be _hasContent.

Copilot uses AI. Check for mistakes.
}
else
{
_hasConent = true;
Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field name _hasConent has a spelling error. It should be _hasContent.

Copilot uses AI. Check for mistakes.
/// </summary>
/// <returns>string</returns>
public string? Build() => stringBuffer.Count > 0 ? string.Join(" ", stringBuffer) : null;
public string? Build() => _hasConent ? _builder.ToString() : null;
Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field name _hasConent has a spelling error. It should be _hasContent.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Utils/CssBuilder.cs:17` </location>
<code_context>
 {
-    private readonly List<string> stringBuffer;
+    private readonly StringBuilder _builder = new();
+    private bool _hasConent;

     /// <summary>
</code_context>

<issue_to_address>
**nitpick (typo):** Typo in variable name '_hasConent'.

Rename '_hasConent' to '_hasContent' for consistency.

Suggested implementation:

```csharp
    private bool _hasContent;

```

If `_hasConent` is referenced elsewhere in the file (e.g., in methods or properties), those references should also be updated to `_hasContent`.
</issue_to_address>

### Comment 2
<location> `test/UnitTest/Utils/CssBuilderTest.cs:24-25` </location>
<code_context>
+        Assert.DoesNotContain("--bb-drawer-width", cut.Markup);
     }

     [Fact]
</code_context>

<issue_to_address>
**suggestion (testing):** No tests for edge cases with empty, null, or whitespace-only strings in AddClass after refactor.

Add tests to confirm AddClass ignores empty, null, and whitespace-only strings without affecting output.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link

codecov bot commented Sep 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7c9a31d) to head (32cc54c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #6799   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          739       739           
  Lines        31743     31748    +5     
  Branches      4467      4464    -3     
=========================================
+ Hits         31743     31748    +5     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(Cssbuilder): improve performance use StringBuilder

2 participants